package com.asiainfo.kbisheet.util

import com.asiainfo.kbisheet.vo.WhereVO;

import groovy.xml.MarkupBuilder;

/**
 * Teradata的数据库工具实现
 * @author kyunra
 *
 */
class DBUTeradata extends DBUCommon{

    String getColumnsXML(String dataBaseName,String tableName){
    	def sql = getSQLInstance()
        
        def req = """
    	select * from dbc.columns where DataBaseName='${dataBaseName}' and TableName='${tableName}' order by ColumnId
    """
        
        def writer = new StringWriter()
        def xml = new MarkupBuilder( writer )
        
        xml.columns {
            sql.eachRow( req as String  ) { row ->
                xml.column(name: trim(row.ColumnName),title: trim(row.ColumnTitle), longTitle:trim(row.ColumnTitle),type: trim(row.ColumnType)) 
            }
        }
        writer.toString()
    }
	
	String getPageSQL(queryVO, columnString,orderString, whereString, from){
		"select ${columnString},row_number() OVER(${orderString}) RN from ${from} ${whereString} QUALIFY RN >=${queryVO.from} AND RN<=${queryVO.to}"
	}
	
	String toDateString(WhereVO whereVO){
		whereVO.column + " " + whereVO.equalSign + " '" + whereVO.value.substring(0, 4) + "-"+ whereVO.value.substring(4, 6) + "-"+ whereVO.value.substring(6, 8) + "'"
	}
	
	String formatDateColumn(String columnName){
		"trim(${columnName} (FORMAT 'yyyymmdd'))"
	}
}
